package com.zheng.business.web.util;

import com.zheng.business.common.constant.BusinessConstant;
import com.zheng.common.util.DateUtil;
import org.apache.commons.lang.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lvg EMIAL：<a href="mailto:weiguo.liang@gh-ca.com">weiguo.liang@gh-ca.com</a> <br>
 * @version 1.0 <br>
 * @Company: GH-CA<br>
 * @ClassName: StatisticsUtil <br>
 * @Description: TODO <br>
 * @date 2019/8/9 10:50<br>
 **/
public class StatisticsUtil {

    /**
     * 获取统计参数
     * @param dateStr 时间字符串
     * @param dateType
     * @return
     */
    public static Map<String,Object> getStatisticsParams(String dateStr, BusinessConstant.StatisticsDateType dateType){
        Date beginTime = null,endTime = null,date = null;
        String unitDateStr = null;
        switch (dateType){
            case DAY:{
                if(StringUtils.isNotBlank(dateStr)){ //日期存在根据具体日期 统计
                    date = DateUtil.parseDate(dateStr,DateUtil.FORMAT_YYYY_MM_DD);
                    beginTime = DateUtil.getBeginTime(date);
                    endTime = DateUtil.getEndTime(date);
                    unitDateStr = dateStr;
                 }else { //不存在 取当前系统时间前一天作为参数
                    dateStr = DateUtil.getCurrentDate(Boolean.TRUE);
                    date = DateUtil.parseDate(dateStr,DateUtil.FORMAT_YYYY_MM_DD);
                    beginTime = DateUtil.getBeginTime(date);
                    endTime = DateUtil.getEndTime(date);
                    unitDateStr = dateStr;
                }
                break;
            }
            case MONTH:{
                if(StringUtils.isNotBlank(dateStr)){ //日期存在根据具体日期 统计
                    date = DateUtil.parseDate(dateStr,DateUtil.FORMAT_YYYY_MM);
                    beginTime = DateUtil.getMonthFirstDayTimeByDate(date,0);
                    endTime = DateUtil.getMonthLastDayTimeByDate(date,0);
                    unitDateStr = dateStr;
                }else{   //不存在，取当前时间的前一个月
                    dateStr = DateUtil.date2Str(new Date(),DateUtil.FORMAT_YYYY_MM);
                    date = DateUtil.parseDate(dateStr,DateUtil.FORMAT_YYYY_MM);
                    beginTime = DateUtil.getMonthFirstDayTimeByDate(date,-1);
                    endTime = DateUtil.getMonthLastDayTimeByDate(date,-1);
                    unitDateStr = DateUtil.formatDate(endTime,DateUtil.FORMAT_YYYY_MM);
                }
                break;
            }
            case YEAR:{
                if(StringUtils.isNotBlank(dateStr)){ //日期存在根据具体日期 统计
                    date = DateUtil.parseDate(dateStr,DateUtil.FORMAT_YYYY);
                    beginTime = DateUtil.getYearFirstDayTimeByDate(date,0);
                    endTime = DateUtil.getYearLastDayTimeByDate(date,0);
                    unitDateStr = dateStr;
                }else{
                    dateStr = DateUtil.date2Str(new Date(),DateUtil.FORMAT_YYYY);
                    date = DateUtil.parseDate(dateStr,DateUtil.FORMAT_YYYY);
                    beginTime = DateUtil.getYearFirstDayTimeByDate(date,-1);
                    endTime = DateUtil.getYearLastDayTimeByDate(date,-1);
                    unitDateStr = DateUtil.formatDate(endTime,DateUtil.FORMAT_YYYY);
                }
                break;
            }
        }

        Map<String,Object> params = new HashMap<>();
        params.put("beginTime",beginTime);
        params.put("endTime",endTime);
        params.put("unitDateStr",unitDateStr);
        return params;
    }
}
